<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/9
 * Time: 15:06
 */

namespace zhtfrontend\components\services\customer;

use yii\web\NotFoundHttpException;
use zhtfrontend\models\datas\ClassManageTime;
use zhtfrontend\models\datas\Classroom;
use zhtfrontend\models\datas\ClassroomGroup;
use zhtfrontend\models\datas\ClassroomGroupRelation;
use zhtfrontend\models\datas\GoodsAttr;
use zhtfrontend\models\datas\GoodsClassTime;
use zhtfrontend\models\datas\OrdersItems;


class Checkclassroom
{
    //订单支付前，检测是否有可用教室-goods表-is_classroom字段==注意一个订单几节课程
    public static function Checkclassroom($order_id)
    {
        $order_items = OrdersItems::find()->where(['orders_id' => $order_id])->all();
        $is_classroom = 1;
        foreach ($order_items as $k => $v) {
            if ($v->goods->is_classroom == 0) {
                $is_classroom = 0;
            }
        }
        return $is_classroom;
    }

    //支付完成时，1、安排教师2、确保相同教室组的其他商品不显示3、注意班级4、发送消息通知家长==注意一个订单几节课程
    public static function Doclassroom($order_id)
    {
        //根据order_id找到order_item
        $order_items = OrdersItems::find()->where(['orders_id' => $order_id, 'teach_type' => 1])->one();
        //根据$order_item->goods->id找到goods_attr表教室号
        $classroom_name = GoodsAttr::find()->where(['goods_id'=>$order_items->goods_id,'obj_type'=>'classroom_name'])->one();
        $classroom_number = GoodsAttr::find()->where(['goods_id'=>$order_items->goods_id,'obj_type'=>'classroom_number'])->one();
        //查询classroom_id
        $classroom_id = Classroom::findOne(['name'=>$classroom_name->value,'room_number'=>$classroom_number->value]);
        //查询classroom是否有分组
        $classroom_group_relation = ClassroomGroupRelation::findOne(['classroom_id'=>$classroom_id->id]);
        if($classroom_group_relation->is_use == 1){
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '该教师已被占用！'));
        }
        //查询classroom_group
        $classroom_group = ClassroomGroup::findOne(['id'=>$classroom_group_relation->classroom_group_id]);

    }


    /**
     * 时间冲突判断
     */
    public static function checkTime($orders_id)
    {
        $items = OrdersItems::find()->where(['orders_id' => $orders_id, 'teach_type' => 1])->one();
        $goodstime = GoodsClassTime::find()->where(['goods_id' => $items->goods_id])->all();
        foreach ($goodstime as $time) {
            $classtime = ClassManageTime::find()->joinWith()->where(['room_id' => $time->room_id, 'class_day' => $time->class_day])->all();
            $goods_start_time = str_replace(':', '', $time->start_time);
            $goods_end_time = str_replace(':', '', $time->start_time);
            if ($classtime != null) {
                foreach ($classtime as $t) {
                    $class_start_time = str_replace(':', '', $t->start_time);
                    $class_end_time = str_replace(':', '', $t->end_time);
                    if ((($class_start_time <= $goods_start_time && $class_end_time > $goods_start_time) || ($class_start_time <= $goods_end_time && $class_start_time >= $goods_start_time)) || (($class_start_time >= $goods_start_time && $class_start_time <= $goods_end_time) || ($class_end_time >= $goods_start_time && $class_end_time < $goods_end_time))) {
                        return 2;
                    }

                }
            }

        }

    }

}
